package poker;

import java.util.*;
import java.io.*;

import exception.HandSizeEx;

public class Human extends Player {

	public Human(String name, int tokens) {
		hand = new ArrayList<Card>(5);
		this.name = name;
		this.tokens = tokens;
		bet = 0;
		hasFolded = false;
		allIn = false;
		disconnected = false;
		wins = 0;
	}

	@Override
	public PlayerAction takeTurn(Phase phase, int bet, boolean allIn) throws HandSizeEx {
		/* if (hand.size() != 5) throw new HandSizeEx();
		for(int i=0; i<5; i++) {
			System.out.println((i+1) + ": " + hand.get(i).getCardTxt());
		}
		System.out.println("Podaj numery kart do wymiany (maksymalnie cztery).");
		System.out.println("Numery oddziel spacją. Wpisz nic lub zero na pierwszym miejscu, jeżeli zostajesz przy obecnej ręce.");
		
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String input = null;
		int exchange[] = new int[4];
		
		while(input == null) {
			try { input = br.readLine(); }
			catch(IOException e) {
				System.out.println("Błąd we/wy!");
				continue;
			}
			try { readInput(exchange, input); }
			catch(Exception e) {
				System.out.println(e.getMessage() + " Spróbuj jeszcze raz:");
				input = null;
			}
		}
		if(exchange[0] == 0) { 
			System.out.println("W porządku.");
			return;
		}
		
		for(int i=0; i<4; i++) {
			if (exchange[i] != 0) hand.remove(exchange[i]-1);
		}
		*/
		return PlayerAction.FOLD;		
	}
	
	public void endTurn() throws HandSizeEx {
		if (hand.size() != 5) throw new HandSizeEx();
		for(int i=0; i<5; i++) {
			System.out.println((i+1) + ": " + hand.get(i).getCardTxt());
		}
		System.out.println("Wciśnij enter, aby zakończyć swoją turę");
		try { 
			System.in.read();
			String ED = "\033[2J";
			String HOME = "\033[H";
			System.out.print(ED + HOME);
			System.out.flush();
		} catch (IOException e) {
			System.out.println("Błąd we/wy");
			System.exit(-1);
		}
		
	}
	
	public void printCards(int i) throws HandSizeEx {
		System.out.println("Gracz " + i);
		if (hand.size() != 5) throw new HandSizeEx();
		for(int j=0; j<5; j++) {
			System.out.println("  " + hand.get(j).getCardTxt());
		}
		System.out.println();
	}
		
	static public void readInput(int[] out, String input) throws Exception {
		int s = out.length;
		int tab[] = new int[s];
		int it = 0;
		
		if (input == null) throw new Exception("Pusta linijka!");
		int l = input.length();
		for(int i=0; i<l; i++) {
			if (Character.isDigit(input.charAt(i))) {
				if (it < s) {
					tab[it] = input.charAt(i)-48;
					if(tab[it] > 5 || tab[it] < 0) {
						throw new Exception("Nieprawidłowy znak " + i + "!");
					}
					else {
						for(int j=0; j<it; j++) {
							if (tab[j] == tab[it]) {
								throw new Exception("Powtórzony znak " + i + "!");
							}
						}
					}
					it++;
				} else {
					throw new Exception("Za dużo kart!");
				}
			} else if (!Character.isWhitespace(input.charAt(i))) {
				throw new Exception("Nieprawidłowy znak " + i + "!");
			}
		}
		
		for(int i=0; i<s; i++) {
			for(int j=0; j<it; j++) {
				if (i == 0) {
					if (tab[j] > out[i]) {
						out[i] = tab[j]; 
					}
				} else if (tab[j] > out[i] && tab[j] < out[i-1]) {
					out[i] = tab[j];
				}
			}
		}
	}
	
	public void exchangeCards(int cards[]) {
		if(cards.length != 4) return;
		int buffer;
		
		for(int i=0; i<4; i++) {
			for(int j=i; j<4; j++) {
				if (i == 0) {
					if (cards[j] > cards[i]) {
						buffer = cards[i];
						cards[i] = cards[j];
						cards[j] = buffer;
					}
				} else if (cards[j] > cards[i] && cards[j] <= cards[i-1]) {
					buffer = cards[i];
					cards[i] = cards[j];
					cards[j] = buffer;
				}	
			}
		}
		
		for(int i=0; i<4; i++) {
			if (cards[i] != 0) hand.remove(cards[i]-1);
		}
		
	}
	
	@Override
	public boolean isBot() {
		return false;
	}
	
	/*
	public int reportCards(int report[]) throws Exception {
		if (report.length != 6) { throw new Exception("Zła długość tabeli!"); }
		if (hand.size() != 5) { throw new HandSizeEx(); }
		boolean color = false, straight = false;
		int three = -1, quad = -1;
		int foundPair[] = { -1, -1 };
		int tab[] = new int[5];
		int buffer;
		
		for(int i=0; i<5; i++) {
			tab[i] = (int)hand.get(i).getCard();
		}
		if (tab[0]/13==tab[1]/13 && tab[1]/13==tab[2]/13 && tab[2]/13==tab[3]/13 
			&& tab[3]/13==tab[4]/13) {
			color = true;
		}
		for(int i=0; i<5; i++) {
			tab[i] %= 13;
		}
		for(int i=0; i<5; i++) {
			for(int j=i; j<5; j++) {
				if (i == 0) {
					if (tab[j] > tab[i]) {
						buffer = tab[i];
						tab[i] = tab[j];
						tab[j] = buffer;
					}
				} else if (tab[j] > tab[i] && tab[j] <= tab[i-1]) {
					buffer = tab[i];
					tab[i] = tab[j];
					tab[j] = buffer;
				}	
			}
		}
		
		if (tab[0]==tab[1]+1 && tab[1]==tab[2]+1 && tab[2]==tab[3]+1 
			&& tab[3]==tab[4]+1) {
			straight = true;
		}
		
		for(int i=0; i<4; i++) {
			if(tab[i]==tab[i+1] && tab[i]!=foundPair[0]) {
				if(i < 2 && tab[i+2]==tab[i+3] && tab[i+2]==tab[i]) {
					quad = tab[i];
				}
				if(foundPair[0] == -1) { foundPair[0] = tab[i]; } 
				else foundPair[1] = tab[i];
			}
			if(i < 3 && tab[i]==tab[i+1] && tab[i+1]==tab[i+2]) {
				three = tab[i];
			}
		}
		
		if (color && straight) { 
			report[0] = 9;
			report[1] = tab[0];
		} else if (quad != -1) {
			report[0] = 8;
			report[1] = quad;
		} else if (three != -1 && (foundPair[0]!=three || foundPair[1]!=-1)) {
			report[0] = 7;
			report[1] = three;
		} else if (color) {
			report[0] = 6;
			for(int i=0; i<5; i++) {
				report[i+1] = tab[i];
			}
		} else if (straight) {
			report[0] = 5;
			report[1] = tab[0];
		} else if (three != -1) {
			report[0] = 4;
			report[1] = three;
		} else if (foundPair[0]!=-1 && foundPair[1] != -1) {
			report[0] = 3;
			if(foundPair[0] > foundPair[1]) {
				report[1] = foundPair[0];
				report[2] = foundPair[1];
			} else {
				report[1] = foundPair[1];
				report[2] = foundPair[0];
			}
			for(int i=0; i<5; i++) {
				if (tab[i] != foundPair[0] && tab[i] != foundPair[1]) {
					report[3] = tab[i];
					break;
				}
			}
		} else if (foundPair[0] != -1) {
			report[0] = 2;
			report[1] = foundPair[0];
			for(int i=2; i<5; i++) {
				for(int j=0; j<5; j++) {
					if (i == 2) {
						if (tab[j] != foundPair[0] && tab[j] > report[i]) {
							report[i] = tab[j]; 
						}
					} else if (tab[j] != foundPair[0] && tab[j] > report[i] && tab[j] < report[i-1]) {
						report[i] = tab[j];
					}	
				}
			}
		} else {
			report[0] = 1;
			for(int i=0; i<5; i++) {
				report[i+1] = tab[i];
			}
		}
		
		return 0;
	}
	*/

}
